package routes

import (
	v1 "zhihu-api/api/v1"
	"zhihu-api/middleware"

	"github.com/gin-gonic/gin"
)

func Setup(r *gin.Engine) {
	// 全局中间件
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	// 无需认证的API
	public := r.Group("/api/v1")
	{
		// 认证相关
		public.POST("/register", v1.Register)
		public.POST("/login", v1.Login)

		// 用户相关
		public.GET("/users", v1.ListUsers)
		public.GET("/users/:id", v1.GetUser)
		public.GET("/users/:id/following", v1.GetUserFollowing)
		public.GET("/users/:id/followers", v1.GetUserFollowers)
		public.GET("/users/:id/collectingAnswers", v1.GetUserCollections)

		// 话题相关
		public.GET("/topics", v1.ListTopics)
		public.GET("/topics/:id", v1.GetTopic)
		public.GET("/topics/:id/followers", v1.GetTopicFollowers)

		// 问题相关
		public.GET("/questions", v1.ListQuestions)
		public.GET("/questions/:id", v1.GetQuestion)
		public.GET("/questions/:id/answers", v1.ListQuestionAnswers)

		// 答案相关
		public.GET("/questions/:id/answers/:answerId", v1.GetAnswer)
	}

	// 需要认证的API
	private := r.Group("/api/v1")
	private.Use(middleware.JWTAuth())
	{
		// 用户相关
		private.PATCH("/users/:id", v1.UpdateUser)
		private.PUT("/users/following/:id", v1.FollowUser)
		private.DELETE("/users/following/:id", v1.UnfollowUser)

		// 话题相关
		private.POST("/topics", v1.CreateTopic)
		private.PATCH("/topics/:id", v1.UpdateTopic)
		private.PUT("/topics/:id/following", v1.FollowTopic)
		private.DELETE("/topics/:id/following", v1.UnfollowTopic)

		// 问题相关
		private.POST("/questions", v1.CreateQuestion)
		private.PATCH("/questions/:id", v1.UpdateQuestion)
		private.DELETE("/questions/:id", v1.DeleteQuestion)
		private.PUT("/questions/:id/following", v1.FollowQuestion)
		private.DELETE("/questions/:id/following", v1.UnfollowQuestion)

		// 答案相关
		private.POST("/questions/:id/answers", v1.CreateAnswer)
		private.PATCH("/questions/:id/answers/:answerId", v1.UpdateAnswer)
		private.DELETE("/questions/:id/answers/:answerId", v1.DeleteAnswer)
		private.PUT("/answers/:id/liking", v1.LikeAnswer)
		private.DELETE("/answers/:id/liking", v1.UnlikeAnswer)
		private.PUT("/answers/:id/collecting", v1.CollectAnswer)
		private.DELETE("/answers/:id/collecting", v1.UncollectAnswer)

		// 评论相关
		private.POST("/questions/:id/comments", v1.CreateQuestionComment)
		private.POST("/answers/:id/comments", v1.CreateAnswerComment)
		private.PATCH("/comments/:id", v1.UpdateComment)
		private.DELETE("/comments/:id", v1.DeleteComment)
		private.POST("/comments/:id/replies", v1.CreateCommentReply)
	}
}
